home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib-tk / tkSimpleDialog.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  273 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Dialog boxes
  5.  
  6. This module handles dialog boxes. It contains the following
  7. public symbols:
  8.  
  9. Dialog -- a base class for dialogs
  10.  
  11. askinteger -- get an integer from the user
  12.  
  13. askfloat -- get a float from the user
  14.  
  15. askstring -- get a string from the user
  16. '''
  17. from Tkinter import *
  18.  
  19. class Dialog(Toplevel):
  20.     '''Class to open dialogs.
  21.  
  22.     This class is intended as a base class for custom dialogs
  23.     '''
  24.     
  25.     def __init__(self, parent, title = None):
  26.         '''Initialize a dialog.
  27.  
  28.         Arguments:
  29.  
  30.             parent -- a parent window (the application window)
  31.  
  32.             title -- the dialog title
  33.         '''
  34.         Toplevel.__init__(self, parent)
  35.         if parent.winfo_viewable():
  36.             self.transient(parent)
  37.         
  38.         if title:
  39.             self.title(title)
  40.         
  41.         self.parent = parent
  42.         self.result = None
  43.         body = Frame(self)
  44.         self.initial_focus = self.body(body)
  45.         body.pack(padx = 5, pady = 5)
  46.         self.buttonbox()
  47.         self.wait_visibility()
  48.         self.grab_set()
  49.         if not self.initial_focus:
  50.             self.initial_focus = self
  51.         
  52.         self.protocol('WM_DELETE_WINDOW', self.cancel)
  53.         if self.parent is not None:
  54.             self.geometry('+%d+%d' % (parent.winfo_rootx() + 50, parent.winfo_rooty() + 50))
  55.         
  56.         self.initial_focus.focus_set()
  57.         self.wait_window(self)
  58.  
  59.     
  60.     def destroy(self):
  61.         '''Destroy the window'''
  62.         self.initial_focus = None
  63.         Toplevel.destroy(self)
  64.  
  65.     
  66.     def body(self, master):
  67.         '''create dialog body.
  68.  
  69.         return widget that should have initial focus.
  70.         This method should be overridden, and is called
  71.         by the __init__ method.
  72.         '''
  73.         pass
  74.  
  75.     
  76.     def buttonbox(self):
  77.         '''add standard button box.
  78.  
  79.         override if you do not want the standard buttons
  80.         '''
  81.         box = Frame(self)
  82.         w = Button(box, text = 'OK', width = 10, command = self.ok, default = ACTIVE)
  83.         w.pack(side = LEFT, padx = 5, pady = 5)
  84.         w = Button(box, text = 'Cancel', width = 10, command = self.cancel)
  85.         w.pack(side = LEFT, padx = 5, pady = 5)
  86.         self.bind('<Return>', self.ok)
  87.         self.bind('<Escape>', self.cancel)
  88.         box.pack()
  89.  
  90.     
  91.     def ok(self, event = None):
  92.         if not self.validate():
  93.             self.initial_focus.focus_set()
  94.             return None
  95.         self.withdraw()
  96.         self.update_idletasks()
  97.         
  98.         try:
  99.             self.apply()
  100.         finally:
  101.             self.cancel()
  102.  
  103.  
  104.     
  105.     def cancel(self, event = None):
  106.         if self.parent is not None:
  107.             self.parent.focus_set()
  108.         
  109.         self.destroy()
  110.  
  111.     
  112.     def validate(self):
  113.         '''validate the data
  114.  
  115.         This method is called automatically to validate the data before the
  116.         dialog is destroyed. By default, it always validates OK.
  117.         '''
  118.         return 1
  119.  
  120.     
  121.     def apply(self):
  122.         '''process the data
  123.  
  124.         This method is called automatically to process the data, *after*
  125.         the dialog is destroyed. By default, it does nothing.
  126.         '''
  127.         pass
  128.  
  129.  
  130.  
  131. class _QueryDialog(Dialog):
  132.     
  133.     def __init__(self, title, prompt, initialvalue = None, minvalue = None, maxvalue = None, parent = None):
  134.         if not parent:
  135.             import Tkinter as Tkinter
  136.             parent = Tkinter._default_root
  137.         
  138.         self.prompt = prompt
  139.         self.minvalue = minvalue
  140.         self.maxvalue = maxvalue
  141.         self.initialvalue = initialvalue
  142.         Dialog.__init__(self, parent, title)
  143.  
  144.     
  145.     def destroy(self):
  146.         self.entry = None
  147.         Dialog.destroy(self)
  148.  
  149.     
  150.     def body(self, master):
  151.         w = Label(master, text = self.prompt, justify = LEFT)
  152.         w.grid(row = 0, padx = 5, sticky = W)
  153.         self.entry = Entry(master, name = 'entry')
  154.         self.entry.grid(row = 1, padx = 5, sticky = W + E)
  155.         if self.initialvalue:
  156.             self.entry.insert(0, self.initialvalue)
  157.             self.entry.select_range(0, END)
  158.         
  159.         return self.entry
  160.  
  161.     
  162.     def validate(self):
  163.         import tkMessageBox as tkMessageBox
  164.         
  165.         try:
  166.             result = self.getresult()
  167.         except ValueError:
  168.             tkMessageBox.showwarning('Illegal value', self.errormessage + '\nPlease try again', parent = self)
  169.             return 0
  170.  
  171.         if self.minvalue is not None and result < self.minvalue:
  172.             tkMessageBox.showwarning('Too small', 'The allowed minimum value is %s. Please try again.' % self.minvalue, parent = self)
  173.             return 0
  174.         if self.maxvalue is not None and result > self.maxvalue:
  175.             tkMessageBox.showwarning('Too large', 'The allowed maximum value is %s. Please try again.' % self.maxvalue, parent = self)
  176.             return 0
  177.         self.result = result
  178.         return 1
  179.  
  180.  
  181.  
  182. class _QueryInteger(_QueryDialog):
  183.     errormessage = 'Not an integer.'
  184.     
  185.     def getresult(self):
  186.         return int(self.entry.get())
  187.  
  188.  
  189.  
  190. def askinteger(title, prompt, **kw):
  191.     '''get an integer from the user
  192.  
  193.     Arguments:
  194.  
  195.         title -- the dialog title
  196.         prompt -- the label text
  197.         **kw -- see SimpleDialog class
  198.  
  199.     Return value is an integer
  200.     '''
  201.     d = _QueryInteger(title, prompt, **kw)
  202.     return d.result
  203.  
  204.  
  205. class _QueryFloat(_QueryDialog):
  206.     errormessage = 'Not a floating point value.'
  207.     
  208.     def getresult(self):
  209.         return float(self.entry.get())
  210.  
  211.  
  212.  
  213. def askfloat(title, prompt, **kw):
  214.     '''get a float from the user
  215.  
  216.     Arguments:
  217.  
  218.         title -- the dialog title
  219.         prompt -- the label text
  220.         **kw -- see SimpleDialog class
  221.  
  222.     Return value is a float
  223.     '''
  224.     d = _QueryFloat(title, prompt, **kw)
  225.     return d.result
  226.  
  227.  
  228. class _QueryString(_QueryDialog):
  229.     
  230.     def __init__(self, *args, **kw):
  231.         if kw.has_key('show'):
  232.             self._QueryString__show = kw['show']
  233.             del kw['show']
  234.         else:
  235.             self._QueryString__show = None
  236.         _QueryDialog.__init__(self, *args, **kw)
  237.  
  238.     
  239.     def body(self, master):
  240.         entry = _QueryDialog.body(self, master)
  241.         if self._QueryString__show is not None:
  242.             entry.configure(show = self._QueryString__show)
  243.         
  244.         return entry
  245.  
  246.     
  247.     def getresult(self):
  248.         return self.entry.get()
  249.  
  250.  
  251.  
  252. def askstring(title, prompt, **kw):
  253.     '''get a string from the user
  254.  
  255.     Arguments:
  256.  
  257.         title -- the dialog title
  258.         prompt -- the label text
  259.         **kw -- see SimpleDialog class
  260.  
  261.     Return value is a string
  262.     '''
  263.     d = _QueryString(title, prompt, **kw)
  264.     return d.result
  265.  
  266. if __name__ == '__main__':
  267.     root = Tk()
  268.     root.update()
  269.     print askinteger('Spam', 'Egg count', initialvalue = 144)
  270.     print askfloat('Spam', 'Egg weight\n(in tons)', minvalue = 1, maxvalue = 100)
  271.     print askstring('Spam', 'Egg label')
  272.  
  273.